using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Gencode.Gengui.Display;

namespace Personalplaner.Modules.Dienstplan
{
    [ToolboxItem(false)]
    public partial class DienstplanContainer : DisplayableView
    {
		///
		/// Indicates if the current view is being utilized in the VS.NET IDE or not.
		///
		public new bool DesignMode
		{
			get
			{
				return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
			}
		}

        DiensplanSelector Content
		{
			get
			{
				return PartnerView as DiensplanSelector;
			}
		}

        List<Plan> planList = new List<Plan>();

        public DienstplanContainer()
        {
            InitializeComponent();

			if (!this.DesignMode)
			{
				//Set Options
				PlanOption.SetData();
			}
        }

        public void TabPage(int planid)
        {
            int listTabIndex = PlanExists(planid);
            if (listTabIndex >= 0)
            {
                tabControl1.SelectedIndex = listTabIndex;
            }
            else
            {
                AddTabPage(planid);
            }
        }

        private void AddTabPage(int planid)
        {
            planList.Add(new Plan(planid, this));

            //TabPage page = new TabPage("");
            Gencode.Gengui.CustomControls.GUTabPage page = new Gencode.Gengui.CustomControls.GUTabPage("");
            tabControl1.TabPages.Add(page);
            tabControl1.SelectedIndex = tabControl1.TabPages.Count - 1;

            planList[planList.Count - 1].Dock = DockStyle.Fill;
            tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(planList[planList.Count - 1]);

            // Set Pagename
            tabControl1.TabPages[tabControl1.TabPages.Count - 1].Text = Gencode.PrsCommon.Data.Plan.PlanData.GetPlanName(planid);
			lblTitle.Text = "Dienstplan - " + tabControl1.TabPages[tabControl1.TabPages.Count - 1].Text;

			//planList[planList.Count - 1].ResetSpliter();
        }

        private int PlanExists(int planid)
        {
            int index = -1;
            for (int i = 0; i < planList.Count; i++)
            {
                if (planList[i].PlanID == planid)
                    index = i;
            }
            return index;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex != -1)
            {
                Content.PlanSel.SetComboPlanIndex(planList[tabControl1.SelectedIndex].PlanID);
				string planname = planList[tabControl1.SelectedIndex].PlanName;

				if (string.IsNullOrEmpty(planname))
					lblTitle.Text = "Dienstplan";
				else
					lblTitle.Text = "Dienstplan - " + planname;
            }
            else
            {
                Content.PlanSel.SetComboPlanIndex(-1);
				lblTitle.Text = "Dienstplan";
            }
        }

        public void CloseTab(int planid)
        {
            if (this.Content.PlanSel.getComboPlanIndex() >= 0)
            {
                planList.RemoveAt(tabControl1.SelectedIndex);
                tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
            }
        }

        public void planAbschliessen(int planid)
        {
			CloseTab(planid);

            Content.PlanSel.SetComboPlan();
        }
    }
}
